package com.estate.web.controller.eastate;

import java.util.ArrayList;
import java.util.List;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.estate.common.utils.redis.RedisUtil;
import com.estate.system.domain.Buy;
import com.estate.system.domain.BuyDetail;
import com.estate.system.service.IBuyDetailService;
import com.estate.system.service.IBuyService;
import com.estate.system.service.ISysAdvantageService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.estate.common.annotation.Log;
import com.estate.common.enums.BusinessType;


import com.estate.common.core.controller.BaseController;
import com.estate.common.core.domain.AjaxResult;
import com.estate.common.utils.poi.ExcelUtil;
import com.estate.common.core.page.TableDataInfo;

import javax.annotation.Resource;

/**
 * 【请填写功能名称】Controller
 *
 * @author estate
 * @date 2022-04-08
 */
@Controller
@CrossOrigin
@RequestMapping("/system/buy")

public class BuyController extends BaseController
{
    private String prefix = "system/buy";

    @Autowired
    private IBuyService buyService;
    @Autowired
    private IBuyDetailService buyDetailService;
    @Autowired
    private ISysAdvantageService advantageService;
    @Resource
    RedisUtil redisUtil;

    @RequiresPermissions("system:buy:view")
    @GetMapping()
    public String buy()
    {
        return prefix + "/buy";
    }

    /**
     *  【跳转到advantage页面】
     * **/
    @GetMapping("/advantage")
    public String toPost()
    {
        return "system/advantage/advantage";
    }

    /**
     * 查询【请填写功能名称】列表
     */

//    @PostMapping("/list")
//    @ResponseBody
//    public TableDataInfo list(Buy buy)
//    {
//
//        List<Buy> list = buyService.selectAllBuyList();
//        return getDataTable(list);
//    }


    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo listByPageHelper(Buy buy, int pageSize, int pageNum)
    {
        //如果这几个其中有一个都不为空则调用数据库的语句去查询
        System.out.println(buy);
            if (!buy.getTitle().equals("") || !buy.getTypehouseArea().equals("") || buy.getPrice() !=null
                || !buy.getAdvantages().equals("") || buy.getIfNeedShow()!=null
            ){
                startPage();
                List<Buy> list = buyService.selectBuyListByMapper(buy);
                System.out.println("1");
                System.out.println(list);
                return getDataTable(list);


            }else {
                //混合pagehelper和redis的分页
                //但是不能像mysql一样做条件查询
                //使用redis分页,需要传参数过去
                System.out.println("2");
                PageInfo<Buy> buyPageInfo = buyService.selectAllBuyList(pageSize, pageNum);
                buyPageInfo.setPageSize(10);
                buyPageInfo.setPageNum(1);
                System.out.println("list:"+buyPageInfo);
                List<Buy> list = buyPageInfo.getList();
                TableDataInfo dataTable = getDataTable(list);
                dataTable.setTotal(buyPageInfo.getTotal());
                System.out.println("buyPageInfo.getTotal()"+buyPageInfo.getTotal());


                return dataTable;
            }

    }
    /**
     * 直接分页
     * **/
    @PostMapping("/list/{pageSize}/{pageNum}")
    @ResponseBody
    public PageInfo<Buy> listByPageHelper(@PathVariable int pageSize,@PathVariable int pageNum)
    {
            //混合pagehelper和redis的分页
            //但是不能像mysql一样做条件查询
            //使用redis分页,需要传参数过去
            System.out.println("2");
            PageInfo<Buy> buyPageInfo = buyService.selectAllBuyList(pageSize, pageNum);
            buyPageInfo.setPageSize(10);
            buyPageInfo.setPageNum(1);

            return buyPageInfo;


    }
    /**
     * 通过title字段模糊查询,并且分页
     *
     * **/
    @PostMapping("/list/title/{pageSize}/{pageNum}")
    @ResponseBody
    public PageInfo<Buy> listByTitlePageHelper(@PathVariable int pageSize,@PathVariable int pageNum,String title)
    {
        
        Buy buy = new Buy();
        buy.setTitle(title);
        PageHelper.startPage(pageNum,pageSize);
        List<Buy> list = buyService.selectByTitleList(buy);
        PageInfo<Buy> pageInfo = new PageInfo<Buy>(list);

        return pageInfo;
    }



    /**
     * 得到上传的图片地址并返回标签,1张图片
     */

    @PostMapping("/selectBuyImg")
    @ResponseBody
    public String afterUploadFileUpdateData(int buyId)
    {
        Buy buy = buyService.selectBuyById((long) buyId);
        System.out.println("buy119"+buy);
        return buy.getImgAddress();
    }
    /**
     * 得到advantage的值,便于回显优点标签数据
     */
    @PostMapping("/getAdvantageBuy")
    @ResponseBody
    public String getAdvantageBuy(int buyId)
    {
        Buy buy = buyService.selectBuyById((long) buyId);

        return buy.getAdvantages();
    }


    /**
     * 导出【请填写功能名称】列表
     */
    @RequiresPermissions("system:buy:export")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Buy buy)
    {
        List<Buy> list = buyService.selectBuyList(buy);
        ExcelUtil<Buy> util = new ExcelUtil<Buy>(Buy.class);
        return util.exportExcel(list, "【请填写功能名称】数据");
    }

    /**
     * 新增【请填写功能名称】
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        mmap.put("advantages", advantageService.selectAdvantageAll());
        return prefix + "/add";
    }

    /**
     * 新增保存【请填写功能名称】
     */
    @RequiresPermissions("system:buy:add")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Buy buy)
    {
        int res=buyService.insertBuy(buy);
        if (res ==1){
            return toAjax(true).put("id",buy.getId());
        }
        return toAjax(false).put("id",0);
    }

    /**
     * 修改【请填写功能名称】
     */
    @RequiresPermissions("system:buy:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        Buy buy = buyService.selectBuyById(id);
        BuyDetail buyDetail = buyDetailService.selectBuyDetailById(id);
        mmap.put("buy", buy);
        mmap.put("buyDetail", buyDetail);
        mmap.put("id",id);
        mmap.put("advantages", advantageService.selectAdvantageAll());
        return prefix + "/edit";
    }
//    @RequiresPermissions("system:buy:edit")
    @GetMapping("/edit_detail/{id}")
    public String edit_detail(@PathVariable("id") Long id, ModelMap mmap)
    {
        //根据id搜索,detail表的数据,不存在则返回null
        Buy buy = buyService.selectBuyById(id);
        mmap.put("buy", buy);
        BuyDetail buyDetail = buyDetailService.selectBuyDetailById(id);
        mmap.put("buyDetail", buyDetail);
        mmap.put("advantages", advantageService.selectAdvantageAll());
        return prefix + "/edit_detail";
    }

    /**
     * 修改保存【请填写功能名称】
     */
    @RequiresPermissions("system:buy:edit")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Buy buy,ModelMap mmap)
    {



        return toAjax(buyService.updateBuy(buy));
    }

    /**
     * 删除【请填写功能名称】
     */
    @RequiresPermissions("system:buy:remove")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(buyService.deleteBuyByIds(ids));
    }
}
